package com.hisoft.web.interceptor;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.core.ApplicationContext;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class RoleInterceptor extends MethodFilterInterceptor {
	private static final long serialVersionUID = -3481160247246525491L;

	private final static String LOGIN = "login";

	protected final Logger logger = Logger.getLogger(getClass());
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {

		@SuppressWarnings("unused")
		ApplicationContext factory = (ApplicationContext) ActionContext
				.getContext()
				.getApplication()
				.get(
						WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		// find userbean
		// UserService userService = (UserService)factory.getBean("userService");
		// get menubean
		
		HttpServletRequest request = ServletActionContext.getRequest();

		Map map = ServletActionContext.getContext().getContextMap();
		String url = request.getRequestURI();
		
		if (null != request.getQueryString())
			url += "?" + request.getQueryString();
		url = url.substring(url.indexOf("/" , 1) + 1);
		map.put("returnurl", url);

		Cookie[] cookies = request.getCookies();

		String username = "";
		String userValidator = "";
		if (cookies != null && cookies.length > 0) {

			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals("username")) {
					username = java.net.URLDecoder.decode(cookies[i].getValue()
							.toString());
				}
				if (cookies[i].getName().equals("userValidator")) {
					userValidator = cookies[i].getValue().toString();
				}
			}
		}

		if (username.equals(""))
			return LOGIN;
		map.put("username", username);

		// check user getSessionUserByUsername
		/*SessionUser user = userService.getSessionUserByUsername(username);
		if (user == null)
			return LOGIN;

		if (!userValidator.equals(user.getHashValidator()))
			return LOGIN;
		map.put("sessionUser", user);

		// ActionSupport m_action = (ActionSupport)invocation.getAction();
		ActionContext m_ActionContext = invocation.getInvocationContext();
		String actionname = m_ActionContext.getName();
		// check run action rule

		//Map param = m_ActionContext.getParameters();

		//String _ctlPageMenu = "";


		if(!user.getUserOperation().equals("-1")){
			
			if (userService.checkRegister(actionname + ".do")) {

				Boolean check = userService.checkOperation(actionname
						+ ".do", user.getUserOperation());
				if (check == false) {
					map
							.put("message",
									"<font color=red><b>hhh</b></font>");
	
					return "message";
				}
			}
		}*/

		return invocation.invoke();
	}
	
}
